package com.glq.book.servlet;

import com.glq.book.entity.User;
import com.glq.book.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private String type = "";
    private UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        type = req.getParameter("type");
        if (type == null) {
            resp.sendRedirect("/user?type=findAll");
        }
        switch (type) {
            case "updateInfo":
                String username = req.getParameter("username");
                String trueName = req.getParameter("trueName");
                String age = req.getParameter("age");
                String tel = req.getParameter("tel");
                boolean b = userService.updateUserInfoByUsername(username, trueName, Integer.valueOf(age), tel);
                if (b) {
                    //成功跳转页面，获取最新信息页面
                    resp.sendRedirect("/user?type=userInfo&username=" + username);
                } else {
                    //失败跳转到失败页面
                    resp.getWriter().write("修改失败");
                }
                break;
            case "updatePass":
                username = req.getParameter("username");
                String password = req.getParameter("password");
                b = userService.updatePasswordByUsername(username, password);
                User user = (User) req.getSession().getAttribute("userInfo");
                if (b) {
                    //成功跳转页面，要求重新登陆

                    if (user.getUsername().equals(username)){
                        req.getSession().setAttribute("userInfo", "");
                        resp.sendRedirect("login.jsp");
                    }else {
                        resp.sendRedirect("/user?type=userInfo&username=" + username);
                    }
                } else {
                    //失败跳转到失败页面
                    resp.getWriter().write("修改密码失败");
                }
                break;

            case "login":
                username = req.getParameter("username");
                password = req.getParameter("password");
                User login = userService.login(username, password);
                if (login != null) {
                    req.getSession().setAttribute("userInfo", login);
                    //跳转至首页
                    resp.sendRedirect("index.jsp");
                } else {
                    //失败跳转到失败页面
                    resp.sendRedirect("login.jsp");
                }
                break;
            case "register":
                username = req.getParameter("username");
                password = req.getParameter("password");
                if (userService.register(username, password)) {
                    //跳转至首页
                    resp.sendRedirect("login.jsp");
                } else {
                    //失败跳转到失败页面
                    resp.sendRedirect("enrol.jsp");
                }
                break;
            default:
                //失败跳转到失败页面
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        type = req.getParameter("type");
        if (type == null) {
            type = "findAll";
        }
        switch (type) {
            case "logout":
                req.getSession().setAttribute("userInfo", "");
                resp.sendRedirect("login.jsp");
                break;
            case "findAll":
                List<User> users = userService.selectUserList();
                req.getSession().setAttribute("userList", users);
                //跳转页面
                resp.sendRedirect("modeUserList.jsp");
                break;
            case "userInfo":
                String username = req.getParameter("username");
                User user = userService.selectUserByUserName(username);
                req.getSession().setAttribute("userInfo_admin", user);
                //跳转页面
                resp.sendRedirect("modeUserInfo.jsp");
                break;
            default:
                //失败跳转到失败页面

        }
    }
}
